﻿using System;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace CommonUtility.Utility
{
    /// <summary>
    /// Json对象处理
    /// </summary>
    public class JsonHelper
    {
        #region Json对象处理
        /// <summary>
        /// 将数据进行Json序列化
        /// </summary>
        /// <param name="data">需要序列化的对象</param>
        /// <returns></returns>
        public static string SerializeData(object data)
        {
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 将json字串转换为T对象
        /// </summary>
        /// <typeparam name="T">转换的对象</typeparam>
        /// <param name="jsonString">json字串</param>
        /// <returns></returns>
        public static T DeserializeData<T>(string jsonString)
        {
            return JsonConvert.DeserializeObject<T>(jsonString);
        }

        public static JObject GetJsonObject(string jsonString)
        {
           return JObject.Parse(jsonString);
        }

        public static string GetJsonObject(JObject jsnValues,string filedName)
        {
            JToken token = jsnValues[filedName];
            if (token != null && token.Type != JTokenType.Null)
                return Convert.ToString(token).Replace("\"", string.Empty);
            return string.Empty;
        }

        /// <summary>
        /// 根据Json更新DataTable中，设置Row的委托
        /// </summary>
        /// <param name="row"></param>
        /// <param name="index"></param>
        public delegate void UpdateDataTable_SetRow(DataRow row, int index);

        /// <summary>
        /// 根据Json更新DataTable
        /// </summary>
        /// <param name="jsonString">Json字符串(必须为DataTable格式的)</param>
        /// <param name="dt">需要更新的DataTable</param>
        /// <param name="columns">需要更新的列</param>
        public static void Json_UpdateDataTable(string jsonString, DataTable dt, UpdateDataTable_SetRow addRow)
        {
            DataTable dtJson = DeserializeData<DataTable>(jsonString);
            int i = 0;
            if (dtJson != null && dtJson.Rows.Count > 0)
            {
                bool isAdd = dt.Rows.Count == 0;
                //对每行进行赋值
                foreach (DataRow updr in dtJson.Rows)
                {
                    DataRow dr = null;
                    if (isAdd)
                    {
                        dr = dt.NewRow();
                        if (addRow != null)
                            addRow(dr, i);
                    }
                    else
                        dr = dt.Rows[i];

                    foreach (DataColumn column in dtJson.Columns)
                        dr[column.ColumnName] = updr[column.ColumnName];

                    i++;
                    if (isAdd)
                        dt.Rows.Add(dr);
                    else
                        isAdd = dt.Rows.Count <= i;
                }
            }

            //清除多余的行
            if (dt.Rows.Count > i)
            {
                for (int j = dt.Rows.Count - i; j > 0; j--)
                    dt.Rows[dt.Rows.Count - j].Delete();
            }
        }
        #endregion
    }
}
